#include "mutex_demo.h"

TSK_INIT_PARAM_S task1;
TSK_INIT_PARAM_S task2;

static VOID TaskF01(void)
{
    UINT32 ret;
    printf("\n++++++++++++++++TaskF01: (1) before MUX pend success, check actual Count = %d, it should be 2\n",Count);
    ret = LOS_MuxPend(g_mutexTest1, LOS_WAIT_FOREVER);
    Count++;
    LOS_TaskDelay(50);
    ret = LOS_MuxPost(g_mutexTest1);
    printf("\n++++++++++++++++TaskF01: (2) before MUX post success, check actual Count = %d, it should be 3\n",Count);
    return;
}

static VOID TaskF02(void)
{
    UINT32 ret;

    // 断言全局变量Count等于 1.
    Count++;
    printf("\n++++++++++++++++TaskF02: (1) before MuxPend check actual Count = %d, it should be 1\n",Count);
    ret = LOS_MuxPend(g_mutexTest1, LOS_WAIT_FOREVER);
    // 断言全局变量Count等于 2.
    Count++;
    printf("\n++++++++++++++++TaskF02: (2) after MuxPend (before MuxPost) check actual Count = %d, it should be 2\n",Count);
    printf("\n++++++++++++++++TaskF02: (3) continue hold mutex for 50 ticks,TaskF01 shoule be scheduled\n");
    LOS_TaskDelay(50); // 继续持有锁 50 ticks 但在此期间TaskF01应该被调度.
    printf("\n++++++++++++++++TaskF02: (4) mux post,TaskF01 will continue exe within the next 50 ticks\n");
    ret = LOS_MuxPost(g_mutexTest1);
    LOS_TaskDelay(10); // delay 10 ticks, TaskF01此时能够获得锁并继续执行.
    // 断言全局变量Count等于 4.
    Count++;
    printf("\n++++++++++++++++TaskF02: (5) after MuxPost check actual Count = %d, it should be 4\n",Count);
}

VOID  Mutex_Demo(VOID)
{
    UINT32 ret;
    //Create the mutex and check the handle
    ret = LOS_MuxCreate(&g_mutexTest1);
    if(ret==LOS_OK)
    {
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili homeworks~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili homeworks~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili homeworks~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili homeworks~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~This is liweili homeworks~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("\n++++++++++++++++Mutex Demo: mutex created ok with handle = 0x%x\n",Count);
    }

    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
    task1.usTaskPrio = TASK_PRIO_TEST ; 
    task1.pcName = "VMuteB2_1";
    task1.uwStackSize = 0x900;
    task1.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID01, &task1);
    if(ret==LOS_OK)
    {
        printf("\n++++++++++++++++Mutex Demo: TaskF01 created ok with ID = 0x%x\n",g_testTaskID01);
    }

    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
    // To set new task priority, it is higher than the TaskF01.
    task2.usTaskPrio = (TASK_PRIO_TEST - 2); 
    task2.pcName = "VMuteB2_2";
    task2.uwStackSize = 0x900;
    task2.uwResved = 0;

    ret = LOS_TaskCreate(&g_testTaskID02, &task2);
    if(ret==LOS_OK)
    {
        printf("\n++++++++++++++++Mutex Demo: TaskF02 created ok with ID = 0x%x\n",g_testTaskID02);
    }

    printf("\n++++++++++++++++Mutex Demo: will delay  90 ticks\n ");
    LOS_TaskDelay(90); // 90 ticks, TaskF01 & TaskF02 should be scheduled within this 90 ticks

    // Here, assert that Count is equal to 4.
    printf("\n++++++++++++++++Mutex Demo: after 90 ticks, finally Count = %d\n",Count);


    ret = LOS_MuxDelete(g_mutexTest1);
    if(ret==LOS_OK)
    {
        printf("\n++++++++++++++++Mutex Demo: g_mutexTest1 deleted ok\n");
    }

    return;
}


